local lspconfig_status_ok, lspconfig = pcall(require, "lspconfig")
if not lspconfig_status_ok then
    return
end

local signs = {
    { name = "DiagnosticSignError", text = '' },
    { name = "DiagnosticSignWarn",  text = '' },
    { name = "DiagnosticSignHint",  text = '' },
    { name = "DiagnosticSignInfo",  text = '' },
}
local signs2 = { -- from trouble
    { name = "DiagnosticSignError", text = '' },
    { name = "DiagnosticSignWarn",  text = '' },
    { name = "DiagnosticSignHint",  text = '' },
    { name = "DiagnosticSignInfo",  text = '' },
    { name = "DiagnosticSignOther", text = '﫠' },
}
local signs3 = { -- from diy
    { name = "DiagnosticSignError", text = '🥵' },
    { name = "DiagnosticSignWarn",  text = '😅' },
    { name = "DiagnosticSignHint",  text = '😂' },
    { name = "DiagnosticSignInfo",  text = '🤤' },
}

for _, sign in ipairs(signs3) do
    vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
end

local config = {
    virtual_text = false, -- virtual text 错误提示 可能影响signature的提示
    signs = {
        active = signs3,   -- show signs
    },
    update_in_insert = true,
    underline = true,
    severity_sort = true,
    float = {
        focusable = true,
        style = "minimal",
        border = "rounded",
        source = "always",
        header = "",
        prefix = "",
    },
}

vim.diagnostic.config(config)

vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded", })
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = "rounded", })

local servers = {
    "lua_ls",
    "clangd",
    "pyright",
    "jsonls",
    "biome",
    "html",
    "cssls",
}

for _, server in pairs(servers) do
    local opts = {
        on_attach = require("user.lsp.handlers").on_attach,
        capabilities = require("user.lsp.handlers").capabilities,
    }

    server = vim.split(server, "@")[1]

    local require_ok, conf_opts = pcall(require, "user.lsp.settings." .. server)
    if require_ok then
        opts = vim.tbl_deep_extend("force", conf_opts, opts)
    end

    lspconfig[server].setup(opts)
end
